/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECovoiturage. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.ui;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;
import com.vaadin.util.ReflectTools;
import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * Formulaire d'identification.
 * 
 * @author sebastien.mouquet
 */
public class LoginForm extends VerticalLayout
                       implements Button.ClickListener
{
    private TextField tfLogin;
    private PasswordField pfPassword;
    private Button btnConnection;
    private Button btnInscription;
    
    
    public LoginForm()
    {
        layout();
    }
    
    
    private void layout()
    {
        tfLogin = new TextField("Identifiant (email)");
        tfLogin.setWidth(200, Unit.PIXELS);
        addComponent(tfLogin);
        
        pfPassword = new PasswordField("Mot de passe");
        pfPassword.setWidth(200, Unit.PIXELS);
        addComponent(pfPassword);
        
        btnConnection = new Button("Connexion");
        btnConnection.addClickListener(this);
        addComponent(btnConnection);
        
        btnInscription = new Button("Créer un compte");
        btnInscription.setStyleName(Runo.BUTTON_LINK);
        btnInscription.addClickListener(this);
        addComponent(btnInscription);
        setComponentAlignment(btnInscription, Alignment.MIDDLE_LEFT);
        
        setMargin(true);
        setSpacing(true);
    }
    
    
    /**
     * Permet de définir la valeur du champ 'identifiant'.<br>
     * Utile par exemple en développement pour mettre une
     * valeur par défaut.
     * 
     * @param login Identifiant à mettre dans le champ correspondant
     */
    public void setLoginValue(String login)
    {
        if(tfLogin != null) {
            tfLogin.setValue(login);
        }
    }
    
    /**
     * Permet de définir la valeur du champ 'mot de passe'.<br>
     * Utile par exemple en développement pour mettre une
     * valeur par défaut.
     * 
     * @param password Identifiant à mettre dans le champ correspondant
     */
    public void setPasswordValue(String password)
    {
        if(pfPassword != null) {
            pfPassword.setValue(password);
        }
    }
    

    @Override
    public void buttonClick(ClickEvent event)
    {
        if(btnConnection.equals(event.getButton()))
        {
            fireEvent(new ConnectionClickEvent(this, tfLogin.getValue(), pfPassword.getValue()));
        }
        else if(btnInscription.equals(event.getButton()))
        {
            fireEvent(new AccountClickEvent(this));
        }
    }
    
    
    public static class ConnectionClickEvent extends Event
    {
        private String login;
        private String password;
        
        public ConnectionClickEvent(Component source, String login, String password)
        {
            super(source);
            this.login = login;
            this.password = password;
        }

        public String getLogin() {
            return login;
        }

        public String getPassword() {
            return password;
        }
    }
    
    public interface ConnectionClickListener extends Serializable
    {
        public static final Method METHOD = ReflectTools.findMethod(ConnectionClickListener.class, "onConnectionClick", ConnectionClickEvent.class);
        
        void onConnectionClick(ConnectionClickEvent event);
    }
    
    public void addConnectionClickListener(ConnectionClickListener listener)
    {
        addListener(ConnectionClickEvent.class, listener, ConnectionClickListener.METHOD);
    }
    
    public void removeConnectionClickListener(ConnectionClickListener listener)
    {
        addListener(ConnectionClickEvent.class, listener, ConnectionClickListener.METHOD);
    }
    
    
    public static class AccountClickEvent extends Event
    {
        public AccountClickEvent(Component source) {
            super(source);
        }
    }
    
    public interface AccountClickListener extends Serializable
    {
        public static final Method METHOD = ReflectTools.findMethod(AccountClickListener.class, "onAccountClick", AccountClickEvent.class);
        
        void onAccountClick(AccountClickEvent event);
    }
    
    public void addAccountClickListener(AccountClickListener listener)
    {
        addListener(AccountClickEvent.class, listener, AccountClickListener.METHOD);
    }
    
    public void removeAccountClickListener(AccountClickListener listener)
    {
        addListener(AccountClickEvent.class, listener, AccountClickListener.METHOD);
    }
}
